public class num495 {
    public int findPoisonedDuration(int[] timeSeries, int duration) {
        if (duration == 0 || timeSeries.length == 0) {
            return 0;
        }
        int l = 0,r = -1;
        int ans = 0;
        for (int i = 0; i < timeSeries.length; i++) {
            if(r != -1 && r < timeSeries[i]) {
                ans += duration;
            }
            if(r != -1 && r >= timeSeries[i]) {
                ans += (timeSeries[i] - l);
            }
            l = timeSeries[i];
            r = l + duration - 1;
        }
        return ans + duration;
    }
}
